import { SchemaManage } from '@/components/SchemaComponent';
import React, { useRef, useMemo } from 'react';
import { connect, history } from 'umi';
import { OutInRecordManageAPI } from '../common/type';
import { ManageTableSwitch } from '../mconfig';
import { ManageServices } from '../services';

import { getKeyRouter } from '@/utils';

const ManageView: React.FC<OutInRecordManageAPI.IMaterialManageView> = ({ curRes, locationProps }) => {
  /**
   * 管理页services
   * */
  const { searchList, reloadRequest, pagination, data, loading } = ManageServices(curRes);

  /**管理页的ref*/
  const ManageViewRef: any = useRef();

  /**
   * 头部权限的切换
   * */
  const switchList: OutInRecordManageAPI.IManageTabsSwitchType = useMemo(() => {
    return [];
  }, [curRes]);

  /**
   * 头部切换方法
   * */
  const onSwitch = (key: string | undefined | number, searchValues: any, params: any) => {
    reloadRequest({ ...params, ...searchValues });
  };

  /**
   * 重置方法
   * */
  const onReset = (reqParams: OutInRecordManageAPI.IAssetTableRequestType) => {
    reloadRequest({ ...reqParams });
  };

  /**
   * 搜索方法
   * */
  const onSearch = (reqParams: OutInRecordManageAPI.IAssetTableRequestType) => {
    reloadRequest({ ...reqParams });
  };

  /**
   * 分页方法
   * */
  const onPageChange = (current: any, pageSize: any, params: any) => {
    reloadRequest({ pageSize, current, ...params });
  };

  /**
   * Table的操作方法
   * */
  const getTableOperateFuncs = {
    onView: (record) => {
      let url: string = '';
      if (record?.type == '0') {
        url = `/assetApplyUes/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`;
      } else if (record?.type == '1') {
        url = `/assetApplyTransfer/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`;
      } else if (record?.type == '2') {
        url = `/assetApplyTransfer/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`;
      } else if (record?.type == '3') {
        url = `/assetApplyReturn/detail/{id}/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`;
      } else if (record?.type == '4') {
        url = `/assetStorage/details/{currentUserID}/:${getKeyRouter()}${getKeyRouter()}`;
      }
      let obj = JSON.parse(JSON.stringify(record));
      obj.id = obj.proofId ? obj.proofId : obj.id;
      history.push({
        pathname: url,
        state: {
          pageType: 'detail',
          record: obj,
          returnPathName: locationProps?.state?.returnPathName,
        },
      });
    },
  };

  /**
   * Table的columns
   * */
  const getTableColumn: Record<string, any>[] = useMemo(() => {
    return ManageTableSwitch(curRes, getTableOperateFuncs);
  }, [curRes]);

  return (
    <>
      <SchemaManage
        ref={ManageViewRef}
        searchProps={{
          switchList,
          searchList,
          onSwitch,
          onReset,
          onSearch,
        }}
        actionList={[]}
        tableProps={{
          columns: getTableColumn,
          dataSource: data?.rows,
          rowKey: 'id',
          loading: loading,
          pagination: pagination as any,
          onPageChange: onPageChange,
        }}
      />
    </>
  );
};

export default connect(({ baseLogin, navTab }: Record<string, any>) => ({
  curRes: baseLogin?.permissions,
  navTab,
}))(ManageView);
